﻿using LPP.Core.Services;
using LPP.Entity.Media;
using LPP.EntityFrameworkCore;
using LPP.Repository.Medias;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;

namespace LPP.Service.MediaService
{
    public class MediaService :IMediaService
    {
        private readonly IMediaRepository _mediaRepository;
        private readonly string _fileStoragePath;
        private readonly SqlDbContext _context;


        public MediaService(IMediaRepository mediaRepository, IConfiguration configuration)
        {
            _mediaRepository = mediaRepository;
            _fileStoragePath = configuration["FileStorage:Path"];
        }
        public async Task<Media> UploadMediaAsync(IFormFile file, string mediaType)
        {
            var fileName = Path.GetRandomFileName();
            var filePath = Path.Combine(_fileStoragePath, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            Media media = mediaType.ToLower() switch
            {
                "image" => new Image
                {
                    FileName = file.FileName,
                    FilePath = filePath,
                    ContentType = file.ContentType,
                    Width = 0, // 设置为适当的值
                    Height = 0 // 设置为适当的值
                },
                "video" => new Video
                {
                    FileName = file.FileName,
                    FilePath = filePath,
                    ContentType = file.ContentType,
                    Duration = TimeSpan.Zero // 设置为适当的值
                },
                _ => throw new ArgumentException("Invalid media type")
            };

            return await _mediaRepository.AddMediaAsync(media);
        }

        public async Task<Media> GetMediaByIdAsync(int id)
        {
            return await _mediaRepository.GetMediaByIdAsync(id);
        }

        public Media GetMedia(int id)
        {
            return _context.Media.Find(id);
        }

        public async Task<List<Media>> GetAllMediaAsync()
        {
            return await _mediaRepository.GetAllMediaAsync();
        }

        public async Task LikeMediaAsync(int id)
        {
            await _mediaRepository.LikeMediaAsync(id);
        }

        public async Task ShareMediaAsync(int mediaId, string platform)
        {
            var share = new Share
            {
                MediaId = mediaId,
                Platform = platform
            };
            await _mediaRepository.ShareMediaAsync(share);
        }
        public void SaveMedia(Media media)
        {
            _context.Media.Add(media);
            _context.SaveChanges();
        }
        public async Task FavoriteMediaAsync(int mediaId, string userId)
        {
            var favorite = new Favorite
            {
                MediaId = mediaId,
                UserId = userId
            };
            await _mediaRepository.FavoriteMediaAsync(favorite);
        }
        public void Add(Media media)
        {
            _context.Media.Add(media);
            _context.SaveChanges();
        }

    }
}
